package com.sogo;

import java.util.ArrayList;   
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.database.Cursor;

public class AddFriend extends Activity{
	
	private ListView listView;
	private Button btnSearch;
	JSONArray jsonArray;
	JSONObject json = new JSONObject();
	DBAdapter db = new DBAdapter(this);
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addfriend);
		Intent s = getIntent();
		String userName;
		userName = s.getExtras().getString("user");
		String firstName;
		firstName = s.getExtras().getString("first");
		String lastName;
		lastName = s.getExtras().getString("last");
		
		listView = (ListView) findViewById(R.id.list);
		listView.setEmptyView(findViewById(R.id.emptyView));
		
		btnSearch = (Button)findViewById(R.id.search);
		/*calls SearchFriendClass that will then return results
		 * I don't think this is needed but will leave for now.
		 * */
		btnSearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), SearchFriend.class);
				startActivity(i);
				finish();
			}
		});

		try{
			json.put("userName", userName);
			json.put("firstName", firstName);
			json.put("lastName", lastName);
			jsonArray=ServerConnection.CommunicateWithServer(json,2); //searching server
			
				

			List<String> pointsList = new ArrayList<String>();
			String[] names;
			names=new String[jsonArray.length()];
			
			String[] userID;
			userID=new String[jsonArray.length()];
			for(int i=0;i<jsonArray.length();i++){
				json=jsonArray.getJSONObject(i);
				names[i]=json.getString("FIRSTNAME")+" "+json.getString("LASTNAME");
				userID[i]=json.getString("ID");
			}
			for(int i=0;i<jsonArray.length();i++){
				pointsList.add(names[i]);
			}
			listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pointsList));
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView parent, View view, int position, long id) {
					String friendID = null; //friend's id
					String userID = null;	//own user id
					Context context = getApplicationContext();

					int duration = Toast.LENGTH_LONG;


					try {
						json=jsonArray.getJSONObject(position);
						friendID=json.getString("ID");	//grab the friends id

						userID=db.getOwnID();
						

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					JSONArray jsonArray1;
					JSONObject json1 = new JSONObject();
					Toast toast = Toast.makeText(context, "friendID "+ friendID + " your id: "+ userID, duration);
					//toast.show();
					try{
						json1.put("friendID", friendID);
						json1.put("userID", userID);
						//Log.e(SoGoAppActivity.class.getName(), json.getString("ID"));
						jsonArray1=ServerConnection.CommunicateWithServer(json1,3);
						
						Intent s = new Intent(getApplicationContext(), FriendsListMainScreen.class);
						startActivity(s);
						finish();
					}
					catch(Throwable t){}
					
				}
			});
				
			
			
	
		
			
			
			
		}catch(Throwable t){};
		}
}
